(function( $ ){
    $( document ).ready( function(){
        var SITE_NAME = 'classic-ua.com';
        var MAIN_TEMPLATE = 'main';
        var FADE_OUT = 700;
        var FADE_IN = 700;

        var currentPage;   // pages/studio/library.json
        var currentPageJSON;
        var previousPageJSON;
        var path;     //   studio/library
        var previousPath;


        function initPage( href ){
            /* if we navigate through site links - do fadeIn and fadeOut */
            var isInnerNav = href ? true : false;

            function parsePath( href ){
                if( !href ) {
                    href = location.href;
                }
                var res = href.match( /\#(.+)$/ );

                path = ( res && res[1] ) ? res[1] : MAIN_TEMPLATE;
                var file = 'pages/' + path + '.json';
                return file;
            }

            currentPage = parsePath( href );

            if( previousPath !== path ) {
                //currentPage = 'pages/main.js';
                //alert(currentPage);
                var jqxhr = $.getJSON( currentPage, function( json ){
                    currentPageJSON = json;
                    console.log( 'page data loaded' );


                    if( isInnerNav ) {
                        $( "#content" ).fadeOut( FADE_OUT, function(){
                            $( this ).html( currentPageJSON.content ).fadeIn( FADE_IN );
                        } );
                    } else {
                        $( "#content" ).html( currentPageJSON.content );
                    }

                    if( isInnerNav ) {
                        $( "#categoryName" ).fadeOut( FADE_OUT, function(){
                            if( currentPageJSON.categoryName ) {
                                $( this ).html( currentPageJSON.categoryName ).fadeIn( FADE_IN );
                            }
                        } );
                    } else {
                        $( "#categoryName" ).html( currentPageJSON.categoryName );
                    }


                    if( currentPageJSON.gallery ) {
                        if( isInnerNav ) {
                            $( "#gallery" ).fadeOut( FADE_OUT, function(){
                                    $( this ).html( currentPageJSON.gallery ).fadeIn( FADE_IN );
                                    gallery();
                                }
                            );
                        } else {
                            $( "#gallery" ).html( currentPageJSON.gallery );
                            gallery();
                        }
                    }

                    if( isInnerNav ) {
                        if( currentPageJSON.logo2Img !== previousPageJSON.logo2Img ) {
                            if( currentPageJSON.logo2Img ) {
                                $( "#logo2" ).fadeOut( FADE_OUT, function(){
                                    $( this ).attr( 'src', currentPageJSON.logo2Img );
                                    $( this ).fadeIn( FADE_IN );
                                } );
                            }
                        }
                    } else {
                        $( "#logo2" ).attr( 'src', currentPageJSON.logo2Img ).css( 'opacity', 1 );
                    }

                    defineMenu();
                    function defineMenu(){
                        var similar = false;
                        var menu = path.split( '/' );
                        if( isInnerNav ) {
                            var previous = previousPath.split( '/' );
                            if( previous[0] === menu[0] ) {
                                similar = true;
                            }
                        }

                        if( menu && menu[0] ) {
                            var menu0 = ($( "#firstMenu a[data-page-id='" + menu[0] + "']" ));
                            $( "#firstMenu a" ).removeClass( "activeMenu" );
                            menu0.addClass( 'activeMenu' );
                            if( !menu[1] ) {
                                if( !similar ) {
                                    changeMenu( currentPageJSON.secondMenu );
                                } else {
                                    $( "#secondMenu a" ).removeClass( 'activeMenu' );
                                }
                            } else {

                                if( !similar ) {
                                    var file = 'pages/' + menu[0] + '.json';
                                    var jqxhr = $.getJSON( file,function( json ){
                                        changeMenu( json.secondMenu, function(){
                                            var menu1 = ($( "#secondMenu a[data-page-id='" + path + "']" ));
                                            $( "#secondMenu a" ).removeClass( "activeMenu" );
                                            menu1.addClass( 'activeMenu' );
                                        } );
                                    } ).fail( function(){
                                            console.log( "can't load parent page for second menu" );
                                        } );
                                } else {
                                    var menu1 = ($( "#secondMenu a[data-page-id='" + path + "']" ));
                                    $( "#secondMenu a" ).removeClass( "activeMenu" );
                                    menu1.addClass( 'activeMenu' );
                                }

                            }
                        }

                    }

                    function changeMenu( secondMenuHTML, callback ){
                        console.log( 'changeMenu' );
                        if( isInnerNav ) {
                            $( "#secondMenu" ).fadeOut( FADE_OUT, function(){
                                $( this ).html( secondMenuHTML ).fadeIn( FADE_IN );
                                $( 'a' ).off( 'click', onClickAJAX ).on( 'click', onClickAJAX );
                                callback && callback();
                            } );
                        } else {
                            $( "#secondMenu" ).html( secondMenuHTML );
                            $( 'a' ).off( 'click', onClickAJAX ).on( 'click', onClickAJAX );
                            callback && callback();
                        }
                    }

                } )
                    .fail( function( data ){
                        console.error( "can't load page data!" );
                        /* TODO load 400! */
                    } );

            }
        }


        /* menu start */
        var onClickAJAX = function( e ){
            /* todo check this */
            previousPath = path;

            previousPageJSON = currentPageJSON;
            var href = $( this ).attr( 'href' );
            if( !href.match( /http\:\/\// ) || ~href.indexOf( SITE_NAME ) ) {
//                if(!~href.indexOf( "#" )) {
//                    e.preventDefault();
//                    history.pushState(href);
//                }
                initPage( href );
            }
        }
        $( 'a' ).on( 'click', onClickAJAX );

        initPage();

//
//        var hack = 0;
//        var prevImg;
//
//
//        for(var i = 0, time = 0; i < imgs.length; i++, time += 1500 ) {
//            (function(ind, timeOut) {
//                setTimeout(function(){
//                    $("#gallery img" ).show();
//                    $("#gallery img" ).attr('src', imgs[ind]).fadeIn(500);
//                    //$("#gallery img" ).animate({"src": imgs[ind]}, 500);
//                    //  alert(ind);
////                    $(img).fadeIn(500);
//                }, timeOut);
//            } )(i,time );
//        };

        function gallery(){

            $( "#gallery img:first" ).show();
            if( $( "#gallery img" ).length > 0 ) {

                var time = 0;
                $( "#gallery img" ).each( function( i ){
                    (function( ind, timeOut, img ){
                        setTimeout( function(){
                            $( img ).fadeIn( 500 );
                        }, timeOut * 1500 );
                    })( i, time, this )
                    time++;
                } );
            }
        }


    } );


})( jQuery );